package cn.wegoteam.shop.adm.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import cn.wegoteam.shop.action.BaseAction;
import cn.wegoteam.shop.inter.AgentServiceInter;
import cn.wegoteam.shop.po.Agent;
import cn.wegoteam.shop.util.DataUtils;
import cn.wegoteam.shop.util.PageBean;

@ParentPackage("manage")
@Namespace("/")
@Controller
public class ManAgentAction extends BaseAction<Agent> {
	@Autowired
	private AgentServiceInter agentService;

	@Action(value = "manageAgentList", results = { @Result(name = SUCCESS, location = MANAGE
			+ "list.jsp") })
	public String manageAgentList() {
		request.setAttribute("type", getParameter("p_type"));
		return SUCCESS;
	}

	@Action(value = "manageLoadAgentList")
	public void manageLoadAgentList() {
		int pageSize = getIntParameter("pageSize");
		int currentPage = getIntParameter("page");
		pageBean = new PageBean();
		pageBean.setSize(pageSize == 0 ? defaultSize : pageSize);
		pageBean.setCurrentPage(currentPage == 0 ? 1 : currentPage);
		pageBean.setTotalPage(getIntParameter("totalPage"));
		System.out.println(agentService.getJsonList(pageBean,
						null, null,
						request));
		writeStringToResponse(agentService.getJsonList(pageBean,
						DataUtils.getMap("type", getParameter("p_type")), null,
						request));
	}

	@Action(value = "manageAddAgent")
	public String manageAddAgent() {
		if(model.getId()!=null){
			agentService.update(model);
		}
		return OPTRESULTSUCCESS;
	}

	@Action(value = "manageFindAgentById", results = { @Result(name = SUCCESS, location = MANAGE
			+ "addAgent.jsp") })
	public String manageFindAgentById() {
		if(model.getId()!=null){
			model=agentService.get(model.getId());
		}
		return SUCCESS;
	}

	@Action(value = "manageDeleteAgent")
	public void manageDeleteAgent() {
		 model=agentService.get(model.getId());
		 writeStringToResponse(agentService.delete(model));
	}
}
